package br.jcrsistemas.syscomercio.dao;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.spi.DirStateFactory.Result;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import br.jcrsistemas.database.HibernateUtility;
import br.jcrsistemas.factory.ConnFactory;
import br.jcrsistemas.syscomercio.bean.Comanda;

public class ComandaDAO implements DAO<Comanda>{
	private Session sessao = HibernateUtility.getSessao();

	@Override
	public void gravar(Comanda instancia) throws HibernateException {
		// Connection conn = ConnFactory.getConnectionFactory().abreConexao();
		// PreparedStatement statement =
		// conn.prepareStatement("INSERT INTO comandas(numero, cliente) VALUES (?,?)"
		// );
		// statement.setInt(1, instancia.getNumero());
		// statement.setString(2, instancia.getCliente());
		// if (statement.execute())
		// return true;
		// return false;
		Transaction tx = sessao.beginTransaction();
		sessao.saveOrUpdate(instancia);
		tx.commit();
		sessao.close();
	}

	@Override
	public boolean atualizar(Comanda instancia) throws SQLException {
		Connection conn = ConnFactory.getConnectionFactory().abreConexao();
		PreparedStatement pstmt = conn.prepareStatement("UPDATE comandas SET cliente = ? WHERE numero = ? ");
		pstmt.setString(1, instancia.getCliente());
		pstmt.setInt(2, instancia.getNumero());
		if(pstmt.execute()) {
			return true;
		}
		return false;
	}

	@Override
	public boolean remover(Comanda instancia) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public ArrayList<Comanda> listarTodos() {
		Session sessao = HibernateUtility.getSessao();
		Criteria criteria = sessao.createCriteria(Comanda.class);
		return new ArrayList<Comanda>(criteria.list());
		
	}

	@Override
	public Comanda procurar(Comanda instancia) throws HibernateException {
		
//		Connection conn = ConnFactory.getConnectionFactory().abreConexao();
//		PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM comandas WHERE numero = ?" );
//		pstmt.setInt(1, instancia.getNumero());
//		ResultSet rs = pstmt.executeQuery();
//		if(rs.next()) {
//			Comanda comanda = new Comanda(rs.getInt("numero"));
//			comanda.setCliente(comanda.getCliente());
//			comanda.setAberta(true);
//			return comanda;
//		}
		Criteria criteria = sessao.createCriteria(Comanda.class);
		criteria.add(Restrictions.eq("numero", instancia.getNumero()));
		
		List resultado = criteria.list();
		if(resultado.size() == 0 ) {
			return null;
		}
		return (Comanda) resultado.get(0);
	}
	
	public void refreshComanda(Comanda comanda ) {
		Comanda newComanda = procurar(comanda);
		if(newComanda == null) {
//			comanda = new Comanda();
			comanda.setAberta(false);
			comanda.setCliente("");
			return;
		}
		comanda.setAberta(true);
		comanda.setCliente(newComanda.getCliente());
	}

}
